home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Return type - array of structures - please help!
- Date: 17 Apr 1996 13:05:17 -0700
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4l3ittINNho9@keats.ugrad.cs.ubc.ca>
- References: <4l0c9g$tsr@thorn.cc.usm.edu> <4l0em9$ubi@thorn.cc.usm.edu>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4l0em9$ubi@thorn.cc.usm.edu>,
- Michael Kirgan <mkirgan@pacific.st.usm.edu> wrote:
- >Michael Kirgan (mkirgan@pacific.st.usm.edu) wrote:
- >
- >BTW, I forgot to mention something. The errors I get are: syntax errors
- >on the function prototype & function header. The other error occured
- >on this statement class = delete(delssn, class); and that error was:
- >operation between types array & struct. Class is an array of structures
- >& like I said before I am trying to make the return type the same.
-
- Ah, but you said in the other posting that ``the above code works''. This
- cannot be said of any program that has syntax errors, since it will not
- translate.
-
- In any case, here is your stuff again:
-
- >typdef struct
- > {
- > char ssn[12];
- > char lastname[15];
- > char firstname[15];
- > char data[10];
- > } student;
-
- So far so good.
-
- >void delete(char [], class [])
-
- Warning: array parameters in C are ``silently'' taken to mean pointers. The
- declaration you want is probably this:
-
- void delete(char ssn[], student class[])
-
- The above declaration is identical to:
-
- void delete(char *ssn, student *class)
-
- There is no way to pass wholesale arrays to, or from, a function in C.
- Structures can be passed and returned, however. If you desperately want to pass
- (fixed size) arrays, you can encapsulate them in structures.
-
- > {
- > student temp[25];
- > /*omitted the body. All it does is delete a struct from the array*/
- > /*temp is the new array with the structure deleteted and it is copied
- > /*back into students*/
- >
- >
- > class = temp;
- > }
-
- You would not write a function in C like the above. It would not be wise. What
- you want is to pass a pointer to the first element of the array, and also tell
- the function the _size_ of the array (the function has no way of knowing how
- big the array is unless you tell it to).
-
- Since you are deleting a student, you want to pass a _pointer_ to the number of
- students, because you will be changing that number, decreasing it by one.
-
- #include <string.h>
-
- void delete_student(char *ssn, student *class, int *num)
-
- {
- int i;
-
- /* search for a matching ssn in array */
-
- for (i = 0; i < *num; i++)
- if (!strcmp(class[i].ssn, ssn)) { /* match! */
- --*num; /* bump down size */
- class[i] = class[*num]; /* move last element in */
- break; /* break out of loop */
- }
- }
-
-
- >main()
- > {
- > student class[25];
- >
- > /*there is now a loop to read in the class info.*/
- > /*next there is a loop to process options, such as printing out the
- > contents of the class, searching for a student in class, deleting,
- > & inserting*/
- >
- > /*I was currently calling delete like this:*/
- > delete(delssn, class);
- > }
-
- You would have to change that to:
-
- main()
- {
- student class[25];
- int clsize = 0; /* class initially empty */
-
- .
- . /* add some students to the class */
- .
-
- delete("1234", class, &clsize); /* del student whose ssn is "1234" */
-
- return 0;
- }
-
- >The above should have the same effect as when I said: class = temp in the
-
- It is not possible to assign to an array variable in C. In an assignment like
-
- class = temp;
-
- The right hand side expression produces a pointer to the first element of array
- temp, rather than an ``array rvalue'' (no such thing). The right side is an
- array lvalue, but it is not a modifiable lvalue. And it's not type compatible
- with the right side anyway...
-
- You can forget about being able to assign arrays or move them around; you can
- only manipulate them as pointers.
-
- About the only operator which treats an array as a fully fledged object is the
- sizeof operator, and the & operator (address of). If you have an array temp of
- 10 characters, then sizeof temp is 10, and &temp produces a pointer to an array
- of 10 char (char (*)[10]), not a pointer to char (char *).
- --
- I'm not really a jerk, but I play one on Usenet.
-